﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;



namespace VideoWallpaper
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private DeskWindow deskWindow = null;
        // 视频存放文件夹（预定义）
        private string videoPath = "WallpaperVideo";
        // 视频壁纸文件路径
        string wallpaperFileName = null;


        /// <summary>
        /// 窗口加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.IO.Directory.Exists(videoPath))
            {
                System.IO.Directory.CreateDirectory(videoPath);
            }
        }

        /// <summary>
        /// 选择壁纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChoiceDeskBg_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog();
            // 初始目录
            // fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            fileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + videoPath;
            fileDialog.Title = "请选择图片或视频";
            //fileDialog.InitialDirectory = videoPath;
            fileDialog.Filter = "图片,视频|*.jpg;*.png;*.gif;*.mkv;*.mp4;*.avi;*.m4v";
            if (!fileDialog.ShowDialog().Value)
            {
                return;
            }
            // 作为壁纸的视频、图片文件的路径
            wallpaperFileName = fileDialog.FileName;
            // 启用开始按钮
            StartDeskBg.IsEnabled = true;
        }


        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartDeskBg_Click(object sender, RoutedEventArgs e)
        {
            // 初始偏移x
            int offset = 0;
            if (deskWindow == null) // DeskWindow窗口不存在
            {
                // 创建 DeskWindow 窗口
                deskWindow = new DeskWindow();
                // 获取所有屏幕
                var allScreens = System.Windows.Forms.Screen.AllScreens;
                // 数组转list
                List<System.Windows.Forms.Screen> screens = new List<System.Windows.Forms.Screen>(allScreens);
                // 升序
                screens.Sort((x, y) => x.Bounds.X.CompareTo(y.Bounds.Y));
                // 遍历所有屏幕，并添加控件（dockpanel、vlcController）到DeskWindow
                foreach (var scr in screens)
                {
                    // 根据x偏移和屏幕宽度高度在deskWindow窗口中添加显示组件
                    deskWindow.AddDisplayComponent(offset, scr.Bounds.Width, scr.Bounds.Height);
                    // 设置偏移量
                    offset += scr.Bounds.Width;
                }

                // 设置总宽度
                deskWindow.Width = offset;
                deskWindow.Height = SystemParameters.PrimaryScreenHeight;   // 主屏幕
                // 播放视频
                deskWindow.PlayVideo(wallpaperFileName);
                deskWindow.Show();  // 显示
                // Tools.SetDeskTop(deskWindow);
                Tools.SetBackGroud(deskWindow);
            }
            else  // 已存在就只更新画面
            {
                // 重新播放
                deskWindow.PlayVideo(wallpaperFileName);
            }
            StartDeskBg.IsEnabled = false;
            ChoiceDeskBg.IsEnabled = false;
            StopDeskBg.IsEnabled = true;
        }


        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopDeskBg_Click(object sender, RoutedEventArgs e)
        {
            if (deskWindow != null)
            {
                deskWindow.Close();
                deskWindow = null;
                ChoiceDeskBg.IsEnabled = true;
                StartDeskBg.IsEnabled = true;
                StopDeskBg.IsEnabled = false;
            }
        }



        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
            Environment.Exit(0);
        }
    }
}
